/*
 * @Author: kanglang
 * @Date: 2021-02-23 13:24:37
 * @LastEditors: 汪滔
 * @LastEditTime: 2021-07-27 13:54:57
 * @Description: 账号安全
 */
import React, { Component } from 'react';
import {
  NativeModules,
  StyleSheet,
  Text,
  TouchableOpacity,
  SafeAreaView,
} from 'react-native';
import api from '@/api';
import { msg, AsyncStorage, SyncStorage, cache } from '@/common';
import { px2dp, isAndroid, mainBgColorLightGray } from '@/styles';
import { ListItem } from '@/components';

import SwtichPhone from './components/switch-phone';

export default class AccountCenter extends Component {
  constructor(props) {
    super(props);
    this.state = {
      certState: 0,
      userInfo: {},
      role: '',
    };
  }

  componentDidMount() {
    this.getDriverCertInfo();
    const role = SyncStorage.getItem(cache.ROLE);
    this.setState({ role });
    AsyncStorage.getItem(cache.USER_INFO).then((userInfo) => {
      this.setState({ userInfo });
    });
  }

  render() {
    const { userInfo, role } = this.state;
    return (
      <SafeAreaView style={styles.container}>
        <SwtichPhone
          style={{ paddingHorizontal: px2dp(32), marginTop: px2dp(24) }}
          onClick={() => {
            this.goToChangeMobile();
          }}
          userInfo={userInfo}
        />
        <ListItem
          title='修改登录密码'
          titleStyle={{ fontSize: px2dp(28) }}
          style={{ paddingHorizontal: px2dp(32), marginTop: px2dp(24) }}
          rightBoxStyle={{}}
          onClick={() => {
            this.goToChangeSecret();
          }}
        />
        {role === 'driver' && (
          <ListItem
            title='人脸信息录入'
            titleStyle={{ fontSize: px2dp(28) }}
            style={{ paddingHorizontal: px2dp(32) }}
            rightBoxStyle={{}}
            onClick={() => {
              this.faceInput();
            }}
          />
        )}
      </SafeAreaView>
    );
  }

  // 查询认证状态
  getDriverCertInfo() {
    api.supply.getDriverCer().then((res) => {
      if (res.success) {
        this.setState({
          certState: res.data.state,
        });
      }
    });
  }

  // 人脸信息录入入口
  faceInput = () => {
    const { certState } = this.state;
    if (certState !== 2) return msg.emit('app:tip', { text: '请先进行司机认证' });
    msg.emit('router: goToNext', { routeName: 'FaceInput' });
  };

  // 改密
  goToChangeSecret = () => {
    const { userInfo } = this.state;
    msg.emit('router: goToNext', {
      routeName: 'InputMobile',
      user: userInfo,
      routerFrom: 'setPwd',
    });
  };

  // 换绑手机号
  goToChangeMobile = () => {
    const { userInfo } = this.state;
    msg.emit('router: goToNext', {
      routeName: 'ChangePhoneConform',
      user: userInfo,
    });
  };
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: mainBgColorLightGray,
    alignItems: 'center',
    position: 'relative',
  },
});
